import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';

import {
  Button,
  DatePicker,
  Form,
  Input,
  Modal,
  Radio,
  Tag,
  Select,
  InputNumber,
  Row,
  Col,
  Checkbox,
} from 'antd';
import { modalLayoutLg, modalLayoutSm } from '@/config/layout';
import { request } from 'umi';
import moment from 'moment';
const { Option } = Select;
const ProductModal = (props: any, refs: any) => {
  const [form] = Form.useForm();
  // const [fileList,setfileList] = useState([]);
  const [measureClassData, setmeasureClassData] = useState([]);
  const { setFieldsValue, resetFields } = form;
  const [code, setCode] = useState('');
  const [isModalVisible, setIsModalVisible] = useState(false);
  //显示弹窗
  const showModal = () => {
    setIsModalVisible(true);
  };

  //取消
  const handleCancel = () => {
    setIsModalVisible(false);
    setCode('');
    resetFields();
    props.callback && props.callback();
  };

  //确定
  const handleOk = () => {
    form
      .validateFields()
      .then((vals) => {
        let Obj = form.getFieldsValue();
        console.log(Obj);

        if (code) {
          Obj.roleId = code;
        } else {
        }
        const url = code ? '/sys/role/editRole' : '/sys/role/editRole';
        request(url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          data: Obj,
        }).then(() => {
          setIsModalVisible(false);
          handleCancel();
        });
      })
      .catch((info) => {});
  };
  const getDetail = (id: any) => {
    request('/sys/role/roleInfo', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: {
        roleId: id,
      },
    }).then((res) => {
      const detaildata = res?.data;
      setFieldsValue(detaildata);
      showModal();
    });
  };
  useImperativeHandle(refs, () => ({
    init: (id: any, record: any) => {
      if (id) {
        setCode(id);
        getDetail(id);
      } else {
        resetFields();
        showModal();
      }

      // setfileList([]);
    },
  }));

  return (
    <>
      <Modal
        title={code === '' ? '新增' : '编辑'}
        visible={isModalVisible}
        onOk={handleOk}
        onCancel={handleCancel}
        centered
        width={700}
        maskClosable={false}
        bodyStyle={{
          maxHeight: window.innerHeight - 150,
          overflowY: 'auto',
        }}
      >
        <Form form={form} layout="vertical" {...modalLayoutSm}>
          <Form.Item
            label="角色名称"
            name="roleName"
            rules={[{ required: true, message: '请输入角色名称' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item label="角色描述" name="roleDescribe">
            <Input />
          </Form.Item>
        </Form>
      </Modal>
    </>
  );
};
const AddMCom = forwardRef(ProductModal);

export default AddMCom;
